package com.example.leetcode.trainingcamp.week1.practice1;

public class Offer58 {

    public String reverseWords(String s) {
        String[] s1 = s.split("\\s+");
        StringBuilder sb = new StringBuilder();
        for (int i = s1.length-1;i>=0;i--){
           if (i != 0){
               sb.append(s1[i]).append(" ");
           }else {
               sb.append(s1[i]);
           }
        }
        return sb.toString().trim();
    }
}

class DemoOffer58{
  public static void main(String[] args) {
    String s = " hello     world!";
    Offer58 o = new Offer58();
    String s1 = o.reverseWords(s);
    System.out.println(s1);
  }
}